package apigenerator.pointcut;

import apigenerator.FacadeApiTypeFilter;

import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;

/**
 * @author 1203056
 * @version Id: FacadeApiPointcut.java, v 0.1 2021-09-08 14:07 1203056 Exp $
 */
public class FacadeApiPointcut implements Pointcut {
    private FacadeApiTypeFilter apiTypeFilter      = new FacadeApiTypeFilter();
    private String              classPackagePrefix = "";

    public FacadeApiPointcut() {
    }

    public FacadeApiPointcut(String classPackagePrefix) {
        this.classPackagePrefix = classPackagePrefix;
    }

    @Override
    public ClassFilter getClassFilter() {
        return clazz -> {
            if (StringUtils.isNotEmpty(classPackagePrefix)) {
                if (!classPackagePrefix.startsWith(clazz.getPackage().getName())) {
                    return false;
                }
            }
            return apiTypeFilter.match(clazz);
        };
    }

    //TODO 重写 equals hashcode tostring

    @Override
    public MethodMatcher getMethodMatcher() {
        return MethodMatcher.TRUE;
    }
}
